<?php 
if(KT!='KhiThe') exit;
class news_controller extends news_model{
		
	public function __construct()
	{
		global $lang;
		$this->lang = $lang;
		$this->_layout = Null;
		
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
		parent::addObject('khithe_images');
		parent::addObject('khithe_paging');
		parent::addObject('khithe_captcha');
	}
	
	public function index_action()
	{
		$resultHtml ='';
		$limit=10;
		$arrhtmlstr = explode('.',$this->getUrlEnd());
		if(!empty($arrhtmlstr[1]))
		{
			/* IF HAS TYPE .HTML */
			$seokey = $this->getUrlNextEnd(1);
			$struri = $this->getUrlFromTo(1,$this->countTotalUrl()-1);
			
		}
		else{
			/* IF NO TYPE .HTML */
			$seokey = $this->getUrlEnd();
			$struri = $this->getUrlFromTo(1);
		} 
		
		$parentID = parent::getIDSk($seokey);		
		$arrMenu = parent::getMenu($parentID);
		$this->lang["title-category"]=$arrMenu['Category']!=""?$arrMenu['Category']:$this->lang['TitleMenu'];
		$totalRows = mysql_num_rows(parent::getSqlnews($parentID,'','')); 
		$strPaging = $this->display_paging($totalRows, intval($this->getUrlEnd()), intval($limit), url_base.$struri.'', $this->lang, '/','.html');
		$this->lang['paging'] = $strPaging; 
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultnew = parent::getSqlnews($parentID, intval($this->getUrlEnd()), intval($limit) );
		
		while( $rownew = mysql_fetch_assoc($resultnew) )
		{ 				
			if($rownew['Picture']){
				$fileImg = path_base.'/uploads/news/'.$rownew['Picture'];			
				$fileThumbImg = path_base.'/uploads/news/cache/'.$rownew['Picture'];
				if(!is_file($fileThumbImg) && is_file($fileImg))
				{
					$this->setIMG($fileImg);
					$this->resizeImage(200, 180, 'auto');
					$this->saveImage($fileThumbImg);
				}
			}
			$stredit ='';
			if(isset($_SESSION['Mod']) && $_SESSION['Mod']=='Admin'){
				$stredit =' | <a style="text-decoration:none;" 
                    href="'.url_base.'admin/news/edit/'.$rownew['ID'].'&iframe=true" 
                    onclick="return hs.htmlExpand(this, {
                        objectType: \'iframe\', width: 800, height: 550,
                        creditsPosition: \'bottom left\', 
                        headingEval: \'this.a.title\',
                        wrapperClassName: \'titlebar\' } )" 
                    title="Edit-News"><small>edit</small></a>';
			}
			
			$logo = $this->getLogo($rownew['FullContents'],$rownew['ID']);
			$logo = ($rownew['Picture']?url_base.'uploads/news/cache/'.$rownew['Picture']:$logo);
			
			$arrnew = array(
				"new-id"=>$rownew['ID'],
				"View"=>$rownew['View'],
				"edit_new"=>$stredit,
				"url-new"=>url_base . ($this->getSkID($rownew['CategoryID'])).'/'.$rownew['SeoKey'].'.html',
				"Title"=>$rownew['Title'],
				"contents-news"=>$this->strShort($rownew['FullContents'],500),
				"logo_new"=>$logo?'<a href="'.url_base . ($this->getSkID($rownew['CategoryID'])).'/'.$rownew['SeoKey'].'.html"><img class="lazy logo_News" alt="'.$rownew['Title'].'" src="'.$logo.'" /></a>':'',
				"datepost"=>$rownew['PostDate']!=''?date("d-m-Y h:m:s", $rownew['PostDate']):'Not',
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['new'],$arrnew);

		} 
		mysql_free_result($resultnew);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlnew'=>$resultHtml));
		$this->lang['dis'] ='';
		$this->lang['dis_errnot'] ='';
		if(empty($totalRows)){
			$this->lang['dis'] = 'disabled="disabled"';
			$this->lang['dis_errnot'] = '<h5 style="color:#F00">'.$this->lang['errnot'].'</h5>';
		}
		
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		
		
		if(!empty($arrMenu['Category']))
		{
			$arrAct = array(
				'html'=>$strHtmlContent,
				'title_paper'=>$arrMenu['TitlePage'],
				'keywords'=> strip_tags($arrMenu['KeyWords']),
				'description'=>$arrMenu['Description'],
				'SeoH1'=>$arrMenu['SeoH1'],
				'SeoH2'=>$arrMenu['SeoH2'],
				'SeoP'=>$arrMenu['SeoP'],
			);

		}else{
			$arrAct = $strHtmlContent;
		}

	
		return $arrAct;
		
	}
	
	public function view_action()
	{
		$this->lang['sms']='';
		$strHtmlContent = $this->khithe_display('detail');
		$arrLink = explode('.',$this->getUrlEnd());
		$rownew = parent::selectnew(trim($arrLink[0]),intval($this->valUrl(2)));
		$stredit ='';
			
		if(isset($_SESSION['Mod']) && $_SESSION['Mod']=='Admin'){
			$stredit =' | <a style="text-decoration:none;" 
				href="'.url_base.'admin/news/edit/'.$rownew['ID'].'&iframe=true" 
				onclick="return hs.htmlExpand(this, {
					objectType: \'iframe\', width: 800, height: 550,
					creditsPosition: \'bottom left\', 
					headingEval: \'this.a.title\',
					wrapperClassName: \'titlebar\' } )" 
				title="Edit-News"><small>edit</small></a>';
		}
				
		$this->lang['dis'] = '';
		$this->lang['dis_same'] = '';
		$StrSame = $this->getlistnew($rownew['CategoryID'],$rownew['ID'],10);
		
		parent::set_view($rownew['View'],$rownew['ID']);
		$arrnew = array(
			"new-id"=>$rownew['ID'],
			"tags"=>'<strong>Tags</strong>: '.$rownew['KeyWords'],
			"edit_new"=>$stredit,
			"url-new"=>url_base . (parent::getSkID($rownew['CategoryID'])).'/'.$rownew['SeoKey'].'.html',
			"url-cat"=>url_base . (parent::getSkID($rownew['CategoryID'])).'/',
			"Title"=>$rownew['Title'],
			"contents-news"=>$this->strFull($rownew['FullContents'],$rownew['ID']),
			"new-img"=>($rownew['Picture']?'<img alt="'.$rownew['Title'].'" src="'.url_base.'uploads/news/'.$rownew['Picture'].'" />':''),
			"datepost"=>$rownew['PostDate']!=''?date("d-m-Y h:m:s", $rownew['PostDate']):'',
			"comments_list"=>$this->getlistcomments($rownew['ID']),
			"news-same-type"=>$StrSame,
		);
		
		$this->lang['txt_Fullname'] = isset($_SESSION['Fullname'])?$_SESSION['Fullname']:NULL;
		$this->lang['YahooMail'] =isset($_SESSION['YahooMe'])?$_SESSION['YahooMe']:NULL;
		
		$strHtmlContent = $this->khithe_assign($strHtmlContent,$arrnew);
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		
		$arrAction = array(
			'html'=>$strHtmlContent,
			'title_paper'=>$rownew['TitlePage'],
			'keywords'=>trim(strip_tags($rownew['KeyWords'])),
			'description'=>$this->getDescription($rownew['Description']),
			'SeoH1'=>$rownew['SeoH1'],
			'SeoH2'=>$rownew['SeoH2'],
			'SeoP'=>$rownew['SeoP'],	
		);
		
		return $arrAction;
	}
	
	public function getlistnew($ID, $newID, $limit)
	{	
		$resultHtml ='';
		$arrhtmlstr = explode('.',$this->getUrlEnd());
		if(!empty($arrhtmlstr[1]))
		{
			/* IF HAS TYPE .HTML */
			$seokey = $this->getUrlNextEnd(1);
			$struri = $this->getUrlFromTo(1,$this->countTotalUrl()-1,'news/');
			
		}
		else{
			/* IF NO TYPE .HTML */
			$seokey = $this->getUrlEnd();
			$struri = $this->getUrlFromTo(1);
		} 
		
		$parentID = parent::getIDSk($seokey);
		$arrMenu = parent::getMenu($parentID);
		$this->lang["title-category"]=$arrMenu['Category']!=""?$arrMenu['Category']:$this->lang['TitleMenu'];		
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultnew = parent::getSqlnews($parentID, intval($this->getUrlEnd()), intval($limit) );
		while( $rownew = mysql_fetch_assoc($resultnew) )
		{ 					
			if($rownew['ID']!=$newID){
			$arrnew = array(
				"new-id"=>$rownew['ID'],
				"url-new"=>url_base.$struri.'/'.$rownew['SeoKey'].'.html',
				"Title"=>$rownew['Title'],
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['list_new'],$arrnew);
			}
		} mysql_free_result($resultnew);
		
		return $resultHtml;
	}
	
	public function getlistcomments($ID)
	{	
		$resultHtml = '';
		if(isset($_POST['Fullname']) && !empty($_POST['Fullname']) && isset($_POST['YahooMail']) ){
			
			if($_POST['Fullname']==''){
				$this->lang['sms'] = 'Hãy nhập tên';
			
			}else if($_POST['YahooMail']==''){
				$this->lang['sms'] = 'hãy nhập yahoo';
			
			}else if($_POST['contents']==''){
				$this->lang['sms'] = 'Hãy nhập nội dung';
				
			}else if($_POST['captcha']!=$_SESSION['Captcha']){
				$this->lang['sms'] = 'Mã bảo mật không giống nhau';
	
			}else{
				parent::setComments($_POST,$ID);
			}
		}
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultcomment = parent::getSqlcomments($ID);
		while( $rowcomment = mysql_fetch_assoc($resultcomment) )
		{ 					
			$arrcomment = array(
				"new-id"=>$rowcomment['ID'],
				"contents-comments"=>$rowcomment['Content'],
				"FulName"=>$rowcomment['FullName'],
				"YahooChat"=>$rowcomment['YahooChat'],
				"datepost"=>date("d-m-Y H:s:i",$rowcomment['PostDate']),
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['list_comments'],$arrcomment);
		} mysql_free_result($resultcomment);
		
		return $resultHtml;
	}
	
	public function index_ajax_action()
	{
		$this->_layout = 'NOT';
		if($this->valUrl(1)=='img')
		{			
			return $this->showImage();
			
		}else{
			return 'notOK';
		}
	}


}
?>